<?php
/* 
 * Author: Quan Van Sinh
 * Email: sinhvnb@vietnambiz.com
 */
function getAllCategory(){
    global $DB;
    $sql="SELECT * FROM article_category";
    $rsAllCategory = $DB->query($sql);
    return $rsAllCategory;
}



function get_cat_tree($parent_id = 0,$space = ' ', $trees = NULL){
    if(!$trees) $trees = array();
    $sql = mysql_query("SELECT * FROM article_category WHERE  parent_id = ".intval($parent_id));
    while($rs = mysql_fetch_assoc($sql)){
        if($parent_id==0){
            $trees[] = array('id'=>$rs['id'],'name'=>$rs['name']);
        }else{
            $trees[] = array('id'=>$rs['id'],'name'=>$space.$rs['name']);
        }
        
        $trees = get_cat_tree($rs['id'],$space.'----',$trees);
    }
    return $trees;
}


function categoryAction(){
    if(isset($_POST['categoryActionType'])){
        $actionType = $_REQUEST['categoryActionType'];
        switch ($actionType) {
            case 'addCategory':
                if(isset($_POST['categoryActionType'])){
                    addCategory();
                }else{
                    getAllCategory();
                }
                break;
            case 'editCategory':
                if(isset($_POST['id'])){
                    editCategory($_POST['id']);
                }else{
                    getAllCategory();
                }
                break;
            case 'updateCategory':
                if(isset($_POST['edId'])){
                    updateCategory($_POST['edId']);
                }else{
                    getAllCategory();
                }
                break;
            case 'deleteCategory':
                if(isset($_POST['id'])){
                    deleteCategory($_POST['id']);
                }else{
                    getAllCategory();
                }
                break;
            default:
                getAllCategory();
        }
    }
}

function addCategory(){
    global $DB;
    $categoryname = filter($_POST['categoryname']);
    $parent_id = intval($_POST['parent_id']);
    $link = CleanFileName($categoryname);
    
    $addCategorySql = "INSERT INTO article_category (parent_id,name,link) VALUES('$parent_id','$categoryname','$link')";
    $DB->query($addCategorySql);
    $_SESSION['rsMessages'] = "Add new category successfull!";
}

function editCategory($id){
    global $DB;
    $editCategorySql = "SELECT * FROM article_category WHERE id = ".intval($id);
    $editRS = $DB->query_first($editCategorySql);
    echo json_encode(array("id"=>$editRS['id'],"parent_id"=>$editRS['parent_id'],"name"=>$editRS['name']));
    die;
}

function updateCategory($id){
    global $DB;
    $categoryname = filter($_POST['edCategoryname']);
    $parent_id = intval($_POST['edParent_id']);
    $link = CleanFileName($categoryname);
    
    $updateCategorySql = "UPDATE article_category SET name='$categoryname', link='$link', parent_id='$parent_id' WHERE id = ".intval($id);
    $DB->query($updateCategorySql);
    $_SESSION['rsMessages'] = "Update category infomations successfull!";
}

function deleteCategory($id){
    global $DB;
    $deleteCategorySql = "DELETE FROM article_category WHERE id = ".intval($id);
    $DB->query($deleteCategorySql);
    $_SESSION['rsMessages'] = "Delete category successfull!";
    die;
}
?>
